<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">登录</block>
		</cu-custom>
		<view class="content margin-top-lg">
			<view class="flex justify-center">
				<image style="width: 150px; height: 150px; background-color: #eeeeee;" mode="aspectFit" src="/static/img/logo.png"></image>
			</view>
			<!-- 			<view class="input-group">
				<view class="input-row border">
					<text class="title">账号：</text>
					<m-input class="m-input" type="text" clearable focus v-model="account" placeholder="手机号"></m-input>
				</view>
				<view class="input-row">
					<text class="title">密码：</text>
					<m-input type="password" displayable v-model="password" placeholder="请输入密码"></m-input>
				</view>
			</view> -->
			<view class="cu-form-group margin-top-lg">
				<view class="title">手机号码</view>
				<input placeholder="输入手机号" name="input" type="number" v-model="account"></input>
				<view class="cu-capsule radius">
					<view class='cu-tag bg-blue '>
						+86
					</view>
					<view class="cu-tag line-blue">
						中国大陆
					</view>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">密码</view>
				<input placeholder="输入密码(6-16)" name="input" type="password" v-model="password"></input>
			</view>
			<view class="btn-row margin-top-lg">
				<button type="primary" class="primary" @tap="bindLogin">登录</button>
			</view>
			<view class="action-row">
				<navigator url="../reg/reg">注册账号</navigator>
				<text>|</text>
				<navigator url="../pwd/pwd">忘记密码</navigator>
			</view>
			<view class="oauth-row" v-if="hasProvider" v-bind:style="{top: positionTop + 'px'}">
				<view class="oauth-image" v-for="provider in providerList" :key="provider.value">
					<image :src="provider.image" @tap="oauth(provider.value)"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var _self
	import service from '../../service.js';
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import mInput from '../../components/m-input.vue'

	export default {
		components: {
			mInput
		},
		data() {
			return {
				providerList: [],
				hasProvider: false,
				account: '',
				password: '',
				smscode: '',
				positionTop: 0,
				smsLoadingStatus:0
			}
		},
		computed: mapState(['forcedLogin']),	
		methods: {
			...mapMutations(['login']),
			initProvider() {
				const filters = ['weixin', 'qq', 'sinaweibo'];
				uni.getProvider({
					service: 'oauth',
					success: (res) => {
						if (res.provider && res.provider.length) {
							for (let i = 0; i < res.provider.length; i++) {
								if (~filters.indexOf(res.provider[i])) {
									this.providerList.push({
										value: res.provider[i],
										image: '../../static/img/' + res.provider[i] + '.png'
									});
								}
							}
							this.hasProvider = true;
						}
					},
					fail: (err) => {
						console.error('获取服务供应商失败：' + JSON.stringify(err));
					}
				});
			},
			
			
			
			initPosition() {
				/**
				 * 使用 absolute 定位，并且设置 bottom 值进行定位。软键盘弹出时，底部会因为窗口变化而被顶上来。
				 * 反向使用 top 进行定位，可以避免此问题。
				 */
				this.positionTop = uni.getSystemInfoSync().windowHeight - 100;
			},
			bindLogin() {
				/**
				 * 客户端对账号信息进行一些必要的校验。
				 * 实际开发中，根据业务需要进行处理，这里仅做示例。
				 */
				if (this.account.length < 5) {
					uni.showToast({
						icon: 'none',
						title: '账号最短为 5 个字符'
					});
					return;
				}
				if (this.password.length < 6) {
					uni.showToast({
						icon: 'none',
						title: '密码最短为 6 个字符'
					});
					return;
				}
				/**
				 * 下面简单模拟下服务端的处理
				 * 检测用户账号密码是否在已注册的用户列表中
				 * 实际开发中，使用 uni.request 将账号信息发送至服务端，客户端在回调函数中获取结果信息。
				 */
				const data = {
					phone: this.account,
					password: this.password,
					smscode: this.smscode
				};
				// const validUser = service.getUsers().some(function (user) {
				//     return data.account === user.account && data.password === user.password;
				// });
				console.log("login with para " + JSON.stringify(data))
				uni.request({
					url: this.api + '/users/login',
					method: 'POST',
					data: data,
					success: res => {
						console.log("login resp")
						console.log(res.data)
						if (res.data.msg != "") {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							});
						} else {
							uni.setStorageSync("XAUTHTOKEN", res.data.data.AuthToken)
							this.cacheAccount(this.account)
							uni.switchTab({
								url: '../home/main',
								success: () => {
									_self.bindCid()
								},
								complete: ()=>{
									uni.$emit('freshLogin')
									uhi.$emit('freshUser')
								}
							})
						}
					}
				});


				// if (validUser) {
				// 	this.toMain(this.account);
				// } else {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '用户账号或密码不正确',
				// 	});
				// }
			},			
			bindCid() {
				var pinf = plus.push.getClientInfo();
				var cid = pinf.clientid;
				console.log("bind cid" + cid);
				if (cid != null && cid != "" && typeof(cid) != "undefined") {
					uni.request({
						url: this.api + '/binds',
						method: 'POST',
						header: {
							"Authorization": "Bearer " + _self.getToken()
						},
						data: {
							cid: cid,
							osName: plus.os.name
						},
						success: res => {
							
						}
			
					});
				} else {
					setTimeout("bindCid()", 2000)
				}
			},
			oauth(value) {
				uni.login({
					provider: value,
					success: (res) => {
						uni.getUserInfo({
							provider: value,
							success: (infoRes) => {
								/**
								 * 实际开发中，获取用户信息后，需要将信息上报至服务端。
								 * 服务端可以用 userInfo.openId 作为用户的唯一标识新增或绑定用户信息。
								 */
								this.toMain(infoRes.userInfo.nickName);
							}
						});
					},
					fail: (err) => {
						console.error('授权登录失败：' + JSON.stringify(err));
					}
				});
			},
			toMain(userName) {
				this.login(userName);
				/**
				 * 强制登录时使用reLaunch方式跳转过来
				 * 返回首页也使用reLaunch方式
				 */
				if (this.forcedLogin) {
					uni.reLaunch({
						url: '../main/main',
					});
				} else {
					uni.navigateBack();
				}

			}
		},
		mounted() {
			_self=this
			this.account = this.getAccount()			
		},
		onReady() {
			this.account = this.getAccount()
			this.initPosition();
			// this.initProvider();
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex: 1;
		flex-direction: column;		
	}

	.my-logo {
		height: 400rpx;
	}

	.action-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
	}

	.action-row navigator {
		color: #007aff;
		padding: 0 20upx;
	}

	.oauth-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
	}

	.oauth-image {
		width: 100upx;
		height: 100upx;
		border: 1upx solid #dddddd;
		border-radius: 100upx;
		margin: 0 40upx;
		background-color: #ffffff;
	}

	.oauth-image image {
		width: 60upx;
		height: 60upx;
		margin: 20upx;
	}

	button.primary {
		background-color: #0faeff;
	}
</style>
